package com.wpi.iCoach;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.SimpleAdapter;
import android.widget.Spinner;
import android.widget.TextView;

/**
 * This activity displays a form for the user to fill-out describing a visit
 * to a location. This is still in the preliminary stages and does not actually
 * update the database. The users is asked to provide three pieces of information
 * for the visit.
 * The data of the visit.
 * How successful they felt the visit was.
 * How much they liked/disliked the service.
 *  
 * @author Kenneth J. Loomis (kjloomis)
 * @version June 2012
 */
public class VisitInfo extends Activity {

	/** Drop-down spinner for the success rating. */
	private Spinner spSuccess;
	
	/** Drop-down spinner for the likeness rating. */
	private Spinner spLike;
	
	/**
	 * Instantiates the activity. This fills the drop-down spinner lists with
	 * options and image icons that are used graphically identify each option. 
	 */
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.visitinfo);

        spSuccess = (Spinner) findViewById(R.id.spSuccess);
        myAdapter adapter1 = new myAdapter(getApplicationContext(), FillSuccessMap ( ),
                R.layout.visititem, new String[] { "Name", "Icon" },
                new int[] { R.id.imagetext, R.id.image });
        spSuccess.setAdapter(adapter1);
        
        spLike = (Spinner) findViewById(R.id.spLike);
        myAdapter adapter2 = new myAdapter(getApplicationContext(), FillLikeMap ( ),
                R.layout.visititem, new String[] { "Name", "Icon" },
                new int[] { R.id.imagetext, R.id.image });
        spLike.setAdapter(adapter2);
	}
	
	/**
	 * Fills an Array List with of Pairs of Success options and icons used by the
	 * drop-down spinners. There are three success options: "Successful", "Neutral",
	 * and "Unsuccessful."
	 * @return An ArrayList of Pairs of Success options and icons used by the
	 * drop-down spinners. 
	 */
	private ArrayList< HashMap < String, Object> > FillSuccessMap ( )
	{
		
        ArrayList<HashMap<String, Object>> list = new ArrayList<HashMap<String, Object>>();
        
        HashMap<String, Object> map = new HashMap<String, Object>();
        map.put("Name", "Successful");
        map.put("Icon", R.drawable.gear_green);
        list.add(map);

        map = new HashMap<String, Object>();
        map.put("Name", "Neutral");
        map.put("Icon", R.drawable.gear_yellow);
        list.add(map);

        map = new HashMap<String, Object>();
        map.put("Name", "Unsuccessful");
        map.put("Icon", R.drawable.gear_red);
        list.add(map);

		return list;
	}
	
	/**
	 * Fills an Array List with of Pairs of Likeness options and icons used by the
	 * drop-down spinners. There are three likeness options: "Liked", "Neutral",
	 * and "Didn't Like."
	 * @return An ArrayList of Pairs of Success options and icons used by the
	 * drop-down spinners. 
	 */
	private ArrayList< HashMap < String, Object> > FillLikeMap ( )
	{
		
        ArrayList<HashMap<String, Object>> list = new ArrayList<HashMap<String, Object>>();
        
        HashMap<String, Object> map = new HashMap<String, Object>();
        map.put("Name", "Liked");
        map.put("Icon", R.drawable.check_green);
        list.add(map);

        map = new HashMap<String, Object>();
        map.put("Name", "Neutral");
        map.put("Icon", R.drawable.check_yellow);
        list.add(map);

        map = new HashMap<String, Object>();
        map.put("Name", "Didn't Like");
        map.put("Icon", R.drawable.check_red);
        list.add(map);

		return list;
	}	

}

/**
 * An adapter used to fill the spinner items with a description and icon pair instead of
 * just a text description.
 * 
 * @author Kenneth J. Loomis (kjloomis)
 * @version June 2012
 */
class myAdapter extends SimpleAdapter {
	
	/** The context of the activity that uses this adapter */
	private Context mcontext;
	
	/**
	 * Constructor simply stores the context, but relies on the 
	 * underlying adapter constructor.
	 */
    public myAdapter(Context context, List<? extends Map<String, ?>> data,
            int resource, String[] from, int[] to) {
        super(context, data, resource, from, to);
        
        mcontext = context;
    }
    
   /** 
    * Gets the drop-down spinner item for the position provided. Meaning it converts a
    * entry in the each entry in the hashmap a graphical representation. First the
    * components of the graphical representation of the entry (visit_item.xml) are bound to
    * the programmatic equivalents. Then, the values of these views are changed to match
    * the values in the hashmap. 
    * (This is not called directly; called by the set adapter for the parent drop-down
    * spinner view.)
    */ 
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
    	LayoutInflater object = (LayoutInflater)mcontext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    	   if (convertView == null) {
               convertView = object.inflate(R.layout.visititem,
                       null);
           }

           HashMap<String, Object> data = (HashMap<String, Object>) getItem(position);

           ((TextView) convertView.findViewById(R.id.imagetext))
                   .setText((String) data.get("Name"));
           ((ImageView) convertView.findViewById(R.id.image))
                   .setImageResource((Integer) data.get("Icon"));

           return convertView;
    }

}
